package com.xzr.hospitalregister.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;

import java.util.Locale;

/**
 * Created by Administrator on 2018/2/5 0005.
 */

public class MyService extends Service{
    private static final String TAG="Test";
    String text1;
    boolean running = false;
    private TextToSpeech textToSpeech;////////
    private String remberName = null;

    @Override
    //Service时被调用
    public void onCreate()
    {
        /////////////
        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == textToSpeech.SUCCESS) {
                    int result = textToSpeech.setLanguage(Locale.CHINA);
                    if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
                            && result != TextToSpeech.LANG_AVAILABLE) {
                        Toast.makeText(MyService.this, "TTS暂时不支持这种语音的朗读！",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        //////////////
        ////////////
        super.onCreate();
        running = true;

        new Thread() {
            public void run() {
                while (running) {
//                    System.out.println(Service中获取到的数据： + data);
                    ///////////
                    textToSpeech.speak(text1,
                            TextToSpeech.QUEUE_ADD, null);
//                    if (remberName == null) {
//                                textToSpeech.speak(text1,
//                                        TextToSpeech.QUEUE_ADD, null);
//                                remberName = text1;
////                        remberName = data.get(0).getHujiao();
//                        Log.e("TAG", "在服务中第一次读+++" + text1);
//                    } else {
//                        if (remberName.equals(text1)) {
//                            textToSpeech.stop();
//                        } else {
//                                    textToSpeech.speak(text1,
//                                            TextToSpeech.QUEUE_ADD, null);
//                            remberName = text1;
//                            Log.e("TAG", "在服务中第二次读不同的+++" + text1);
//                        }
//                    }
                    ////////////
                    ////////////
//                    textToSpeech.speak(text1,
//                                        TextToSpeech.QUEUE_ADD, null);
//                    Log.i(TAG, "在服务中读出的语音内容--->"+text1);
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
        }.start();
        //////////////
        Log.i(TAG, "Service onCreate--->");
        super.onCreate();
    }

    @Override
    //当调用者使用startService()方法启动Service时，该方法被调用
    public void onStart(Intent intent, int startId)
    {
        ///////////////
        text1 = intent.getStringExtra("text1");
//        return super.onStart(intent,startId);
        //////////////
        Log.i(TAG, "显示在服务中的语音内容--->"+intent.getStringExtra("text1"));
        super.onStart(intent, startId);
    }

    @Override
    //当Service不在使用时调用
    public void onDestroy()
    {
        /////////
        running = false;
        /////////
        if (textToSpeech != null)
            textToSpeech.shutdown();
        /////////////
        Log.i(TAG, "Service onDestroy--->");
        super.onDestroy();
    }

    @Override
    //当使用startService()方法启动Service时，方法体内只需写return null
    public IBinder onBind(Intent intent)
    {
        return null;
    }

}
